<?php
/**
 * I苹果打包
 *
 *
 */
class ipack extends main
{
    const VERSION_RELEASE  = 1;
    const VERSION_DEBUG    = 2;

    const STATUS_ONE       = 1;
    const STATUS_TWO       = 2;
    const STATUS_SWH       = 3;
    const STATUS_FOR       = 4;


    function __construct()
    {
        parent::__construct();
        $this->loadModel('g5_iiapple_pack');
        $this->version = self::_getVersion();
        $this->status  = self::_getStatus();
        $this->tag     = self::_getIiappleVer();
        $this->branch  = self::_getbranch();
    }

    function index()
    {
        $page 	  = isset($_GET[2]) ? max(1, $_GET[2]) : 1;
        $pageSize = 20;
        $search = array(
            //'searchPak'   => $_GET['searchPak'],
            //'searchPer'   => $_GET['searchPer'],
            'date_stat'   => isset($_POST['date_stat']) ? strtotime($_POST['date_stat']) : '',
            'date_end'    => isset($_POST['date_end']) ? strtotime($_POST['date_end']) : '',
            'version'     => isset($_POST['Searchannel']) ? $_POST['Searchannel'] : '',
            'branch'      => isset($_POST['SearStatus']) ? $_POST['SearStatus'] : '',
            'tag'         => isset($_POST['SearStatus']) ? $_POST['SearStatus'] : '',
            'status'      => isset($_POST['SearStatus']) ? $_POST['SearStatus'] : '',
        );

        $string = isset($_GET[2]) ? $_GET[2] : '';
        if($string != ''){
            $search = unserialize(base64_decode($string));
        }

        $where    = array();
        if(!empty($search['date_stat']) && !empty($search['date_end'])) {
            $where['addTime >= '] = $search['date_stat'];
            $where['addTime <= '] = $search['date_end'];
        }

        $result   = $this->g5_iiapple_pack->limit($pageSize,($page-1)*$pageSize)->getAll('*', $where);
        //echo $this->g5_cron_job->last_query();
        $total    = $this->g5_iiapple_pack->getCount('*', $where);


        $this->page($page, $total, NULL, $pageSize);
        $this->search       = $search;
        $this->result       = $result;
    }

    function add()
    {
        if($_POST['button']) {
            $version = $_POST['version'];
            $tag     = $_POST['tag'];
            $branch  = $_POST['branch'];


            $data = array(
                'version'   => $version,
                'branch'    => $branch,
                'tag'       => $tag,
                'status'    => self::STATUS_ONE,
                'addTime'   => time(),
                'addUser'   => $_SESSION['adminmanager']['user_true_name']
            );

            $this->g5_iiapple_pack->insert($data);
            $this->report('添加任务成功', BASEURI.'ipack/index');
        }
    }


    function edit()
    {
        $id = isset($_GET[2]) ? max(1, $_GET[2]) : '';

        if(empty($id) && !$_POST['button']) {
            $this->report('传递参数错误', BASEURI.'ipack/index');
        }
        $data = $this->g5_iiapple_pack->getOne('*', array('id' => $id));
        if($_POST['button']) {
            $version = $_POST['version'];
            $tag     = $_POST['tag'];
            $branch  = $_POST['branch'];


            $data = array(
                'version'   => $version,
                'branch'    => $branch,
                'tag'       => $tag,
                'status'    => self::STATUS_ONE,
                'addTime'   => time(),
                'addUser'   => $_SESSION['adminmanager']['user_true_name']
            );

            $this->g5_iiapple_pack->insert($data);
            $this->report('添加任务成功', BASEURI.'ipack/index');
        }
        $this->data       = $data;
        $this->formAction = BASEURI. 'ipack/edit';
        $this->display('ipack.add');
    }

    function delete()
    {
        echo "delete";
    }

    function pack()
    {
        $id = isset($_GET[2]) ? max(1, $_GET[2]) : '';

        if(empty($id)) {
            $this->report('要执行打包的任务不选在', BASEURI.'ipack/index');
        }

        $select = $this->g5_iiapple_pack->getCount('*', array('status' => self::STATUS_TWO));

        if($select != 0) {
            $this->report('有正在执行的任务 请稍后再试', BASEURI.'ipack/index');
        }

        $pack = $this->g5_iiapple_pack->getOne('*', array('id' => $id));
        $this->g5_iiapple_pack->update(array('status' => self::STATUS_TWO), array('id' => $id));
        $cmd = "sh ".ROOT_PATH."web/build.sh ".$this->version[$pack['version']]." ".$this->branch[$pack['branch']]." ".$this->tag[$pack['tag']]. " ".IIAPPLE;

        exec($cmd, $actions, $cobak);

        if($cobak != 0){
            $this->g5_iiapple_pack->update(array('status' => self::STATUS_SWH), array('id' => $id));
            return false;
        };

        if(is_file(IIAPPLE.'iiapplefilename')) {
            $file = file_get_contents(IIAPPLE.'iiapplefilename');
            if(!is_file(IIAPPLE.$file))
            {
                $this->g5_iiapple_pack->update(array('status' => self::STATUS_SWH), array('id' => $id));
                $this->report('打包失败', BASEURI.'ipack/index');
            }
            $data = array(
                'status'   => self::STATUS_FOR,
                'trueTime' => time(),
                'packName' => $file,
                'packSize' => format_bytes(filesize(IIAPPLE.$file))
            );
            $this->g5_iiapple_pack->update($data, array('id' => $id));
            $this->report('打包成功', BASEURI.'ipack/index');
        }

    }

    private function _getVersion()
    {
        return array(
            self::VERSION_RELEASE  => 'Release',
            self::VERSION_DEBUG    => 'Debug'
        );
    }

    private function _getbranch()
    {
        return array(
            1 => 'master',
            2 => 'develop_Pod'
        );
    }

    private function _getStatus()
    {
        return array(
            self::STATUS_ONE => '新建任务',
            self::STATUS_TWO => '正在打包',
            self::STATUS_SWH => '打包失败',
            self::STATUS_FOR => '打包成功'
        );
    }

    private function _getIiappleVer()
    {
        return array(
            1 => '1.3.3',
            2 => '1.3.5'
        );
    }
}